package socket.server;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Date;

/**
 * 
 * @auth hucc
 * @date 2015年10月10日
 */
public class WeatherThread extends Thread {
    
    private Socket socket;
    
    public WeatherThread(Socket socket){
        this.socket = socket;
    }
    
    public void run() {
        
        DataInputStream dataInputStream = null;
        DataOutputStream dataOutputStream = null;
        try {
            // 接受客户端请求
            dataInputStream = new DataInputStream(socket.getInputStream());
            String request = dataInputStream.readUTF();
            String threadName = Thread.currentThread().getName();
            System.out.println("from client..." + request+" 当前线程："+threadName);
            
            // 响应客户端
            dataOutputStream = new DataOutputStream(socket.getOutputStream());
            if(Integer.parseInt(threadName.substring(threadName.length()-1, threadName.length()))/2==0){
            	
            	Date now = new Date();
            	Date when = new Date(now .getTime() + 60000);
            	while(when.after(now)){
            		now = new Date();
            	}
            }
            String response = "天气：晴朗，温度：36度";
            dataOutputStream.writeUTF(response);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (dataInputStream != null) {
                    dataInputStream.close();
                }
                if (dataOutputStream != null) {
                    dataOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}